package org.rsbot.event.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import org.rsbot.bot.Bot;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Methods;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;
import org.rsbot.script.Calculations;
import org.rsbot.script.wrappers.RSItemTile;

public class DrawGround implements PaintListener {

    private final Methods ctx;

    public DrawGround(final Bot bot) {
        this.ctx = bot.getMethods();
    }

    public void onRepaint(final Graphics render) {
        if (!ctx.isLoggedIn())
            return;
        final RSPlayer player = ctx.getMyPlayer();
        if (player == null)
            return;
        render.setColor(Color.WHITE);
        final RSTile location = player.getLocation();
        for (int x = location.getX() - 25; x < location.getX() + 25; x++) {
            for (int y = location.getY() - 25; y < location.getY() + 25; y++) {
                final RSItemTile[] item = ctx.getGroundItemsAt(x, y);
                if ((item == null) || (item.length == 0)) {
                    continue;
                }
                final Point screen = ctx.calc.tileToScreen(item[0]);
                if (!ctx.pointOnScreen(screen)) {
                    continue;
                }
                render.drawString("" + item[0].getItem().getID(), location.getX() - 10, location.getY());
            }
        }
    }
}
